package y24.m5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Temperature {
    public static void main(String[] args) {
        JFrame frame = new JFrame("温度转换");
        JLabel cLabel = new JLabel("摄氏度：");
        JTextField cField = new JTextField(10);
        JLabel fLabel = new JLabel("华氏度：");
        JTextField fField = new JTextField(10);
        JButton button = new JButton("转换");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                double celsius = Double.parseDouble(cField.getText());
                double fahrenheit = (celsius * 9 / 5) + 32;
                fField.setText(String.valueOf(fahrenheit));
            }
        });
        JPanel panel = new JPanel(new GridLayout(2, 2));
        panel.add(cLabel);
        panel.add(cField);
        panel.add(fLabel);
        panel.add(fField);
        frame.add(panel,BorderLayout.NORTH);
        frame.add(button,BorderLayout.SOUTH);
        frame.setLocationRelativeTo(null);
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
